<?php
/**
 * Delete Action deletes a file from its fid (database)
 *   $_REQUEST['fid']
 */
class DeleteAction extends CAction {
    
    public function run() {
        //if (Yii::app()->request->isAjaxRequest) {
            if (isset($_REQUEST['fid'])) {
                // Delete from database
                $file = File::model()->findByPk($_REQUEST['fid']);
                if (isset($file)) {
                    if ($file->delete()) {
                        // Delete file
                        UploadUtils::deleteFile($file, SimpleUploadWidget::$fileDir);
                        $result["result"] = 0;
                    } else {
                        $result["result"] = -1;
                        $result["message"] = Yii::t('amo', 'Could not delete file from db');
                    }
                } else {
                    $result["result"] = -1;
                    $result["message"] = Yii::t('amo', 'No file with that fid');
                }
            } else {
                $result["result"] = -1;
                $result["message"] = Yii::t('amo', 'There is no file id (nor db and no session)');
            }
            echo CJSON::encode($result);
            exit(0); // To avoid loggers append things to request
        //}
    }
}
